/**************************************
This program generates estimates used in Figure 5

These estimates seare also used to generate Table A6

Regressions are state x cohort x year postgrad 
Storing estimates in nbias_scatter_barrons_byyear_[date].dta

Author: Andrew Foote
Date: 11/19/2020


**************************************/
#delimit ;
set more off; 

include "./config.do" ;
include "./drbvars.do" ; 

use "$datadir/all_earnings_long.dta", clear; 

des;

tab year_grad;

/*************************************
First, keep only people
who aren't missing demographics.
*************************************/


include "./restrictions.do" ;

tab ui_state ;

include "./top_school.do" ;


local demogs "male white hispanic black asian " ;

/* defining missing as no in-state earnings, so that
it matches what researchers have */ 
gen missing = instate_earnings ==0 ;

postfile bias_scatter beta_in se_in in_N intop_N beta_out se_out out_N outtop_N  bias missing_diff missing_N missingtop_N	str2 state grad_year year_post 
                                using $supportdir/bias_scatter_barrons_byyear_$date.dta, replace;
tab opeid top_school, m ; 
tab ui_state top_school, m ;
tab ui_state year_grad, m ;
tab opeid if top_school == . ; 
levelsof ui_state, local(states) ;

foreach state in `states' { ;
   forvalues cohort = 2001/2013 { ;
       forvalues ypg = 1/10 { ;
	      count if ui_state == "`state'" & year_grad == `cohort' & year-year_grad == `ypg' &
                       (instate_earnings != 0 | national_earnings != 0) ;

              /********************************************
              Inserting a breaker here to not run for NY, cohort = 2001
              ********************************************/
              if "`state'" == "36" & `cohort' == 2001 { ;
                                                        count if year_grad == 3000 ;
                                                    } ;
                              
              di "============================================" ;
              di "STATE: `state'     " ;
              di "Year Grad: `cohort'    " ;
              di "Year Postgrad: `ypg'   " ;
              di "Observation counts: " r(N) ;
              di "============================================" ;
              if r(N) > 0  { ; 
			capture noisily reg log_earn_instate top_school `demogs' 
								if ui_state == "`state'" & year_grad == `cohort'
                                   & year-year_grad == `ypg', vce(cluster pik);
                        count if e(sample) ;
                          if r(N) > 0 { ; 
                               local beta_in = _b[top_school];
			       local se_in = _se[top_school];
                          } ;
                          else { ;
                               local beta_in = . ;
			       local se_in = . ; 
                          } ; 
                          local in_N =  r(N) ;
                        count if e(sample) & top_school == 1 ;
                          local intop_N = r(N) ;
			  
			capture noisily reg log_earn_national top_school `demogs' 
								if ui_state == "`state'" & year_grad == `cohort'
                                   & year-year_grad == `ypg', vce(cluster pik) ;
                             count if e(sample) ;
                             if r(N) > 0 { ;
                                  local beta_out = _b[top_school];
				  local se_out = _se[top_school];
                             } ;
                             else { ;
                                  local beta_out = . ;
				  local se_out = . ; 
                             };  
                             local out_N =  e(N) ;
			 count if e(sample) & top_school == 1 ;
                           local outtop_N = r(N) ;
			capture noisily reg missing top_school `demogs' 
								if ui_state == "`state'" & year_grad == `cohort'
                                   & year-year_grad == `ypg', vce(cluster pik) ;
                          count if e(sample) ;
                          local missing_N = r(N) ;                             
                          if r(N) > 0 { ; 
                                local missing_diff = _b[top_school] ;
                          } ;
                          else { ;
                                local missing_diff = . ;
                          } ; 
			  local bias = `beta_out' - `beta_in' ;
                          count if e(sample) & top_school== 1 ;
                             local missingtop_N = r(N) ;
			  
                          post bias_scatter (`beta_in') (`se_in') (`in_N') (`intop_N') (`beta_out') (`se_out') (`out_N') (`outtop_N') (`bias') 
						 (`missing_diff') (`missing_N') (`missingtop_N') ("`state'") (`cohort') (`ypg') ;
                         }; 
		} ;
	} ;
} ; 

postclose bias_scatter;
